

package com.zdkj.subway.stm.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.crypto.digest.MD5;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.stm.annotation.SwStmNoRepeatSubmitBaseController;
import com.zdkj.subway.stm.constant.StmTableCommon;
import com.zdkj.subway.stm.dto.in.SwStInputPassengerWorkDTO;
import com.zdkj.subway.stm.entity.SwStInputPassengerWorkRefund;
import com.zdkj.subway.stm.entity.SwStmStationDataEntryLog;
import com.zdkj.subway.stm.service.SwStInputPassengerWorkRefundService;
import com.zdkj.subway.stm.service.SwStmStationDataEntryLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * 乘客事务录入明细表【故障退款】
 *
 * @author cuilei
 * @date 2020-05-25 13:38:07
 */
@RestController
@AllArgsConstructor
@RequestMapping("/swstinputpassengerworkrefund")
@Api(value = "swstinputpassengerworkrefund", tags = "乘客事务录入明细表【故障退款】管理")
public class SwStInputPassengerWorkRefundController extends SwStmNoRepeatSubmitBaseController {

    private final SwStInputPassengerWorkRefundService swStInputPassengerWorkRefundService;
    private final SwStmStationDataEntryLogService swStmStationDataEntryLogService;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 分页查询
     *
     * @param page                         分页对象
     * @param swStInputPassengerWorkRefund 乘客事务录入明细表【故障退款】
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public R getSwStInputPassengerWorkRefundPage(Page page, SwStInputPassengerWorkRefund swStInputPassengerWorkRefund) {
        return R.ok(swStInputPassengerWorkRefundService.page(page, Wrappers.query(swStInputPassengerWorkRefund)));
    }

    /**
     * 通过id查询乘客事务录入明细表【故障退款】
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Integer id) {
        return R.ok(swStInputPassengerWorkRefundService.getById(id));
    }

    /**
     * 通过关联id查询故障退款明细
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过关联id查询故障退款明细", notes = "通过关联id查询故障退款明细")
    @GetMapping("/getById/{id}")
    public R getByRelationId(@PathVariable("id") Integer id) {
        return R.ok(swStInputPassengerWorkRefundService.list(Wrappers.<SwStInputPassengerWorkRefund>query()
                .lambda().eq(SwStInputPassengerWorkRefund::getRelationId, id)));
    }

    /**
     * 新增乘客事务录入明细表【故障退款】
     *
     * @param workDTO 乘客事务录入明细表【故障退款】
     * @return R
     */
    @ApiOperation(value = "新增乘客事务录入明细表【故障退款】", notes = "新增乘客事务录入明细表【故障退款】")
    @SysLog("新增乘客事务录入明细表【故障退款】")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('stm_sw_st_input_passenger_work_add')")
    public R save(@RequestBody SwStInputPassengerWorkDTO<SwStInputPassengerWorkRefund> workDTO) {
        String md5 = MD5.create().digestHex(workDTO.toString());
        if (noPass(SecurityUtils.getUser().getId(), "stmRefund", md5)) {
            return R.failed("请勿频繁操作");
        }
        boolean b = swStInputPassengerWorkRefundService.saveInputWork(workDTO);
        if(b){
            return R.ok();
        }else {
            return R.failed("已提交的数据不能再添加");
        }
    }

    /**
     * 修改乘客事务录入明细表【故障退款】
     *
     * @param workDTO 乘客事务录入明细表【故障退款】
     * @return R
     */
    @ApiOperation(value = "修改乘客事务录入明细表【故障退款】", notes = "修改乘客事务录入明细表【故障退款】")
    @SysLog("修改乘客事务录入明细表【故障退款】")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('stm_sw_st_input_passenger_work_edit')")
    public R updateById(@RequestBody SwStInputPassengerWorkDTO<SwStInputPassengerWorkRefund> workDTO) {
        String md5 = MD5.create().digestHex(workDTO.toString());
        if (noPass(SecurityUtils.getUser().getId(), "stmRefund", md5)) {
            return R.failed("请勿频繁操作");
        }
        return R.ok(swStInputPassengerWorkRefundService.updateInputWork(workDTO));
    }


    /**
     * 通过乘客事务主表id查询故障退款历史记录
     *
     * @param  id
     * @return R
     */
    @ApiOperation(value = "通过乘客事务主表id查询故障退款历史记录", notes = "通过乘客事务主表id查询故障退款历史记录")
    @GetMapping("getLogById/{id}")
    public R getLogById(@PathVariable("id") Integer id) {
        List<Object> LogInfoList = new ArrayList<>();
        List<SwStmStationDataEntryLog> swStmStationDataEntryLogs = swStmStationDataEntryLogService.getBaseMapper()
                .selectList(Wrappers.<SwStmStationDataEntryLog>lambdaQuery()
                        .eq(SwStmStationDataEntryLog::getRelationId, id)
                        .eq(SwStmStationDataEntryLog::getLogTableName, StmTableCommon.SW_ST_INPUT_PASSENGER_WORK_REFUND));
        //返回json串
        if(CollectionUtil.isNotEmpty(swStmStationDataEntryLogs)){
            swStmStationDataEntryLogs.stream().forEach(x -> {
                JSONObject jsonObject = JSONObject.parseObject(x.getLogInfo());
                LogInfoList.add(jsonObject);
            });
        }
        //List<String> LogInfoList = swStmStationDataEntryLogs.stream().map(SwStmStationDataEntryLog::getLogInfo).collect(Collectors.toList());
        return R.ok(LogInfoList);
    }

    /**
     * 通过id删除乘客事务录入明细表【故障退款】
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除乘客事务录入明细表【故障退款】", notes = "通过id删除乘客事务录入明细表【故障退款】")
    @SysLog("通过id删除乘客事务录入明细表【故障退款】")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('stm_sw_st_input_passenger_work_refund_del')")
    public R removeById(@PathVariable Integer id) {
        return R.ok(swStInputPassengerWorkRefundService.removeById(id));
    }

    @Override
    public RedisTemplate<String, String> redisTemplate() {
        return redisTemplate;
    }
}
